package com.example.nonacademic_education.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.nonacademic_education.data.request.Graduate;
import com.example.nonacademic_education.data.request.StudentExcelVo;
import com.example.nonacademic_education.data.request.StudentList;
import com.example.nonacademic_education.data.response.*;
import com.example.nonacademic_education.entity.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @author xinmiao
 * @description
 * @date 2023/3/28 13:33
 */
public interface StudentDao extends BaseMapper<Student> {
    @Select("select * from student where identity_card_id=#{identityCardId} limit 1")
    StudentDto getOneStudent(String identityCardId);

    @Insert("insert into student (identity_card_id, password,class_id, name, sex, enrollment_time, graduate_time, identification_photo_url) " +
            "values (right(#{identityCardId},8),'$2a$10$v5lfYLc7YLDyfMRy8X0hnePc0z9LhkH4nGa1Z1OtMPnC.bvhuSAt6',#{classId},#{name},#{sex},#{enrollmentTime},#{graduateTime},#{identificationPhotoUrl});")
    Boolean addOne(StudentDto student);

    @Update("update student set certificate_no = #{certificateNo},creation_time = #{creationTime} where identity_card_id=#{identityCardId} ;")
    void updateByidentityCardId(StudentDto student);

    List<StudentDto> getStudentList(StudentList studentList);

    Integer getNumber(StudentList studentList);

    @Update("update student set is_del = 1 where id = #{id} ;")
    void deleteStudent(Long id);

    void updateStudent(Student student);

    List<GraduateInfoDto> getDiplomaByCardId(String identityCardId, Long studentId);

    Integer addStudentBatch(@Param("students") List<StudentImportDto> students, Long classId, Date enrollmentTime, Date graduateTime);

    List<GraduateInfoDto> getGraduateInfo(Long classId);

    @Select("select count(id) from student where class_id=#{classId}  and is_del=0")
    Integer countGraduateInfo(Long classId);

    Integer updateGraduateInfo(Date creationTime, @Param("graduates") List<Graduate> graduates);

    @Select("select id from student where class_id=#{classId} and is_del=0 order by id")
    List<Long> getClassStudentIds(Long classId);

    @Select("select * from student where identity_card_id=#{username} and password=#{password} limit 1")
    StudentUserDto getOneByUsernameAndPassword(String username, String password);

    @Update("update student set identification_photo_url=#{identificationPhotoUrl} where identity_card_id=#{identityCardId}")
    Integer uploadHeadImage(String identityCardId, String identificationPhotoUrl);

    @Select("select * from student where identity_card_id=#{username} order by id desc limit 1")
    StudentUserDto getOneByUsername(String username);

    @Select("select * from student where identity_card_id=#{username} and identification_photo_url is not Null order by id desc limit 1")
    StudentUserDto getOneHaveUrl(String username);

    @Update("update student set identification_photo_url=#{identificationPhotoUrl} where identity_card_id=#{identityCardId} and identification_photo_url is not Null")
    Integer updateUrlByUsername(String identityCardId, String identificationPhotoUrl);

    @Update("update student set password=#{password} where identity_card_id=#{identityCardId}")
    Integer resetPassword(String identityCardId, String password);

    @Select("select * from student where identity_card_id=#{username} and password!=#{password} limit 1")
    Student getOneByUsernameAndPassword1(String username, String password);

    @Select("select count(id) from student where class_id=#{classId} and certificate_no is null and is_del=0")
    Integer getNoGraduateInfo(Long classId);

    @Select("select * from student where id=#{id}")
    StudentDto getOneStudentById(Long id);

    List<StudentExcelDto> exportStatisticsExcel(StudentExcelVo studentExcelVo);

    @Select("select id from student where class_id=#{classId} and certificate_no is null and is_del=0 order by id")
    List<Long> getClassStudentIdsNoGraduate(Long classId);

    @Update("update student set creation_time=#{creationTime},certificate_no=concat(left(certificate_no,length(certificate_no)-8),#{year},right(certificate_no,4)) where class_id=#{classId}")
    Integer updateCreationTime(Integer year, Date creationTime, Long classId);

    @Select("select count(id) from student where class_id=#{classId} and certificate_no is not null")
    Integer getGraduatedNumber(Long classId);

    @Select("select count(id) from student where class_id=#{classId}")
    Integer getClassStudentIdsNumber(Long classId);

    @Select("select password from student where identity_card_id=#{username} and is_del=0 order by id limit 1")
    String getPasswordByUsername(String username);

    @Update("update student set is_del=1 where class_id in (select tc.id from theclass tc where tc.project_id=#{id})")
    Integer deleteByProjectId(Long id);

    @Update("update student set is_del=1 where class_id =#{id}")
    Integer deleteByClassId(Long id);

    @Select("select max(right(certificate_no,4)) from student where class_id = #{classId}")
    String getMaxNoThisClass(Long classId);

    Integer addStudentBatchGraduted(@Param("students") List<StudentImportDto> cachedDataList, Long classId, Date enrollmentTime, Date graduateTime, Date creationTime);

    @Select("")
    List<Student> getContacts(String name);

    @Update("update student set able=#{able} where identity_card_id=#{id}")
    Integer enableAccount(String id, Integer able);
}
